package com.sherlocky.common;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.json.JsonUtil;
import org.springframework.web.filter.OncePerRequestFilter;

import com.rework.joss.persistence.convention.ConventionUtils;
import com.sherlocky.config.constants.ConfigConstants;
import com.sherlocky.config.dao.ConfigDAO;

import conf.AppContextHolder;

public class SherlockyFilter  extends OncePerRequestFilter{

	private static String APP_ENCODING = "_app_encoding_tag_";
	
	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		SecurityContextHolder.setContextPath(request);
		
//		if(Constants.VERSION == null) {
//			Constants.VERSION = (AppContextHolder.getContext().getBean(ConfigDAO.class).getValue("version"));
//		}
//		request.setAttribute("VERSION", Constants.VERSION);
		// setSiteInfo(request, SecurityFacade.getUserBaseInfo());
		//处理get方式自动转码
		String tag = request.getParameter(APP_ENCODING);
		if (StringUtils.isBlank(tag)
				|| "POST".equalsIgnoreCase(request.getMethod())) {
			filterChain.doFilter(request, response);
		} else {
			HttpServletRequest req = new AppRequestWrapper(request, null);
			filterChain.doFilter(req, response);
		}
		SecurityContextHolder.clearContext();
	}

	public static void setSiteInfo(HttpServletRequest request) {
		//处理top header是否选中样式
		String path = request.getServletPath();
		String module = StringUtils.removeStart(StringUtils.substringAfter(path, "/"), "admin/");
		int index = module.indexOf("/") > -1 ? module.indexOf("/") : (module.indexOf("?") > -1 ? module.indexOf("?") : module.length()); 
		String activtClass = StringUtils.substring(module, 0, index);
		request.setAttribute("activeClass", ConventionUtils.m(activtClass, "active"));
		
		String siteTitle = null;
		String siteLogo = request.getContextPath() + "/static/images/logo.png";
//		if(!StringUtils.contains(path, ".")) {
//			request.setAttribute("currentUserInfo", JsonUtil.toStr(user));
//		}
		if(StringUtils.isEmpty(siteTitle)) {
			siteTitle = AppContextHolder.getContext().getBean(ConfigDAO.class).getValue("site-title");
		}
		if(StringUtils.isBlank(ConfigConstants.VERSION)) {
			ConfigConstants.VERSION = AppContextHolder.getContext().getBean(ConfigDAO.class).getValue("version");
		}
		if (StringUtils.isBlank(ConfigConstants.STATIC_URI)) {
			ConfigConstants.STATIC_URI = AppContextHolder.getContext().getBean(ConfigDAO.class).getValue("static-uri");
		}
		
		request.setAttribute("siteTitle", siteTitle);
		request.setAttribute("siteLogo", siteLogo);
		request.setAttribute("VERSION", ConfigConstants.VERSION);
		request.setAttribute("staticURI", ConfigConstants.STATIC_URI);
	}
}
